angular.module('starter.services',[])



.factory('StatementService', function($http, ApiEndpoint) {

	var URL= "http://192.168.0.7:8080/tesis/statements/";
	
	return {
		getAll: function(){
			
//		var req = {
//				 method: 'GET',
//				 url: 'http://192.168.0.7:8080/tesis/statements/',
//				 headers: {
//			   'Authorization': 'Basic dGVzdDp0ZXN0',
//               'Content-Type': 'application/x-www-form-urlencoded'
//				 }
//				 }
//		

			//TODO: agregar parametro de usuario
			return $http.get("http://192.168.0.7:8080/tesis/statements/").success(function(data, status, headers, config){
				
		//	return $http(req).success(function(data, status, headers, config){
		
			}).error(function(data, status, headers, config) {
					console.log('data' + status);
				}
				); 
			
		},
		// +  "&voidedStatementId=" + "&agent=" + "&=verb=" +"&activity=" + "&registration=" + "&related_activities=" + "&related_agents=" + "&since=" +"&until=" + "&limit=" +  "&format=" + "&attachments=" +  "&ascending="
		get: function(statementId){
			return $http.get( URL + "?statementId=" + statementId).success(function(data, status, headers, config){	});
			
		},
		
		getXGroup: function(groupId){
			return $http.get( URL  + 'listStatements/' + groupId).success(function(data, status, headers, config){
				
			}).error(function(data, status, headers, config) {
					console.log('data' + status);
				}
				); 
		},
		
		put:function(data){
			return $http.put( URL + '?statementId=0',JSON.stringify(data), {headers:{'Content-Type':'application/json'}})
				.error(function(data, status, headers, config) {
						console.log("Error en PUT  " + status);
				});
		},
		
		putExtension:function(statementParentId,data){
			return $http.post( URL + 'addExtension/' + statementParentId,JSON.stringify(data), {headers:{'Content-Type':'application/json'}})
				.error(function(data, status, headers, config) {
						console.log("Error en PUT  " +status);
				});
			
		},
		
		createDao:function($scope){
			var data = {
				"id":"0",
				"actor":{
					 "id":"19",
					 "objectType":"AGENT"},
				"verb":{
					"id":"2"},
				"object":{	
					"id":"22222",
					"name": $scope.statement.nombre,
					"description": $scope.statement.descripcion,
					"objectType":"ACTIVITY"},
				"objectType":"STATEMENT",
				"context":{
					"team":{
						"id":"18",
						"objectType":"GROUP"}
				},
				"attachment":{
					"fileURL":$scope.statement.adjunto
				}
				};
			return data;	
		}
	
	}
})

.factory('ActorService', function($http) {

	var URL= "http://192.168.0.7:8080/tesis/actor/";
	var user;
	
	return {
		getAllGroup: function(){
			//TODO: Agegar para agente, para solo traer los grupos del agente.
			return $http.get( URL + 'getGroups').success(function(data, status, headers, config){
			
			}).error(function(data, status, headers, config) {
					console.log('Error al recupera los grupos por usuario.');
					console.log('S' + status);
				}
			); 
			
		},
		
		login:function(){
			//TODO
			
		}
		
	}
})

.factory('UtilService', function($http){
	
	
	return {
		//Parser para youtube
		parseYoutube: function(url){
			var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
			var match = url.match(regExp);
			if (match&&match[7].length==11){ 			return match[7]; 		}
			}
	}
})
	


;